package fefteen;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.util.Random;

import javax.swing.*;

public class code extends JFrame
{
	int rows = 3;
	int cols = 3;
	private JPanel panel;
	private JPanel panel2;
	private JOptionPane op;

	public code() throws InterruptedException
	{
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setSize(500, 500);
		
		panel = new JPanel();
		this.getContentPane().add(panel);
		panel.setLayout(null);
		
		final JButton bt = new JButton("Start");
		bt.setSize(150,150);
		bt.setLocation(200, 200);
		panel.add(bt);
		
		bt.addActionListener(new ActionListener() 
		{
			
			
			@Override
			public void actionPerformed(ActionEvent arg0)  
			{
				// TODO Auto-generated method stub
				try {
					bt.setVisible(false);
					create();
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
	}
	void create() throws InterruptedException
	{
		createBufferStrategy(3);
		
		final JButton massM [][] = new JButton[4][];
		massM[0]= new JButton [4];
		massM[1]= new JButton [4];
		massM[2]= new JButton [4];
		massM[3]= new JButton [4];
				
		
		
		int t = 1;
		
		
		
		ActionListener wr = new ActionListener() 
		{
int iii;
			@Override
			public void actionPerformed(ActionEvent arg0) {
								
				// TODO Auto-generated method stub
				
					a: for (int it=0;it<4;it++)
					{
						for(int j=0;j<4;j++)
						{
							
								if(massM [it][j]==arg0.getSource())
								{
									if((Math.abs(it - rows) == 1 && j == cols) ||
									   (Math.abs(j - cols) ==1 && it == rows))
									{
										massM[rows][cols]= massM[it][j];
										massM[it][j]=null;
										rows=it;
										cols=j;
										break a;
									}
								}
						}
					}
			
				//if(massM [it][j]==arg0.getSource()||)
				
				
				for(int it=0;it<4;it++)
				{
					for(int j=0;j<4;j++)
					{
						if(massM [it][j]!=null)
						{
							massM [it][j].setLocation(j*101, it*101);
							
						}
					}
				}
				int be=0;
				
				b: for(int it=0;it<4;it++)
				{
					for(int j=0;j<4;j++)
					{
						
						if(massM [it][j]!=null)
						{
							int beb=Integer.parseInt(massM [it][j].getText()); 
							be++;
							if (be!=beb)
							{
								break b;
							}
						}
						else
							break b;
					}
				}
				if(be==15)
					op.showMessageDialog(null, "You Win!!!");
			}
			private Container getContentPane() {
				// TODO Auto-generated method stub
				return null;
			}
			
			
		};
		
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				if (j!=cols || i!=rows)
				{

				
				massM [i][j]= new JButton(String.valueOf(t));
				massM [i][j].setLocation(j*101, i*101);
				massM [i][j].setSize(100, 100);
				massM [i][j].addActionListener(wr);
				panel.add(massM [i][j]);
				
				t++;
				}
			}
		}
		

		for(int it=0;it<4;it++)
		{
			for(int j=0;j<4;j++)
			{
				if(massM [it][j]!=null)
				{
					panel.add(massM [it][j]);
				}
			}
		}
		Random rand = new Random();

		BufferStrategy bs = getBufferStrategy();
		for(int i=0;i<100;i++)
		{
			int q = rand.nextInt(4);
			
			if (q==0)
			{
				if(rows>0)
				{
					massM[rows][cols] = massM[--rows][cols];
					massM[rows][cols]=null;
				}
				
			}
			
			if (q==1)
			{
				if(rows<3)
				{
					massM[rows][cols]=massM[++rows][cols];
					massM[rows][cols]=null;
				}
			}
			
			if (q==2)
			{
				if(cols>0)
				{
					massM[rows][cols]=massM[rows][--cols];
					massM[rows][cols]=null;
				}
			}
			
			
			if (q==3)
			{
				if(cols<3)
				{
					massM[rows][cols]=massM[rows][++cols];
					massM[rows][cols]=null;
				}
			}			
			
			
			for(int it=0;it<4;it++)
			{
				for(int j=0;j<4;j++)
				{
					if(massM [it][j]!=null)
					{
						massM [it][j].setLocation(j*101, it*101);
						
					}
				}
			}
			
			this.update(bs.getDrawGraphics());
			bs.show();
			
			Thread.sleep(1);
			
			
		}
		
	}
}



